package com.hotel.room.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.hotel.common.param.RenewRoomAdd;
import com.hotel.common.param.RoomOrderAdd;
import com.hotel.common.vo.R;
import com.hotel.room.service.feign.VipFeign;
import com.hotel.user.entity.HotelRoomOrder;
import com.hotel.room.mapper.HotelRoomMapper;
import com.hotel.room.mapper.HotelRoomOrderMapper;
import com.hotel.room.service.HotelRoomOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * <p>
 * 酒店客房预订表 服务实现类
 * </p>
 *
 * @author trc
 * @since 2024-06-17
 */
@Service
public class HotelRoomOrderServiceImpl extends ServiceImpl<HotelRoomOrderMapper, HotelRoomOrder> implements HotelRoomOrderService {

    @Autowired
    private HotelRoomMapper hotelRoomMapper;

    @Autowired
    private HotelRoomOrderMapper hotelRoomOrderMapper;

    @Autowired
    private VipFeign vipFeign;

    /**
     * 创建订单
     *
     * 1.判断当前房间是否已被预约
     * 2.初始化订单其他属性
     * 3.将预约订单添加到订单表中
     *
     * @param roomOrderAdd
     * @return
     */
    @Override
    public HotelRoomOrder createRoomOrder(RoomOrderAdd roomOrderAdd) {

        HotelRoomOrder hotelRoomOrder = new HotelRoomOrder();

        // 根据roomId查询房间状态status 1.未预约 2.已预约
        Integer roomId = roomOrderAdd.getRoomId();
        Integer result = hotelRoomMapper.selectStatusById(roomId);
        if (result == 1) {
            hotelRoomOrder.setRoomId(roomId);
            hotelRoomOrder.setUserId(roomOrderAdd.getUserId());
            // 1表示预约新房
            hotelRoomOrder.setType(1);
            hotelRoomOrder.setInfo("预约新房");

            // 获取当前用户的vip信息,feign远程接口调用
            R my = vipFeign.my();
            // 判断vip中my方法返回的R中data的数据类型
            // 字符串说明有错,是map说明是正常流程
            if (!(my.getData() instanceof String)) {
                LinkedHashMap<String, Object> map = (LinkedHashMap<String, Object>) my.getData();
                Integer value = (Integer) map.get("type");
                if (value == 1 || value == 2 || value == 3) {
                    hotelRoomOrder.setVipType(value);
                } else {
                    hotelRoomOrder.setVipType(0);
                }
            } else {
                hotelRoomOrder.setVipType(0);
            }

            hotelRoomOrder.setPayType(roomOrderAdd.getPayType());

            // 创建订单号
            String no = "r" + System.currentTimeMillis() + RandomUtil.randomNumbers(6);
            hotelRoomOrder.setRoomNo(no);
            hotelRoomOrder.setStatus(1);

            // 字符串解析成时间对象
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            try {
                // 根据用户选择的开始时间进行传值
                hotelRoomOrder.setSdate(format.parse(roomOrderAdd.getSdate()));
                // 根据用户选择的结束时间进行传值
                hotelRoomOrder.setEdate(format.parse(roomOrderAdd.getEdate()));
            } catch (ParseException e) {
                e.printStackTrace();
            }

            // 前端选择开始时间和结束时间后,也会传来天数
            hotelRoomOrder.setDays(roomOrderAdd.getDays());
            // 订单创建时间
            hotelRoomOrder.setCtime(new Date());

            int i = hotelRoomOrderMapper.insert(hotelRoomOrder);
            if (i <= 0) {
                return null;
            }

            return hotelRoomOrder;
        }

        return null;
    }

    @Override
    public HotelRoomOrder createRenewOrder(RenewRoomAdd renewRoomAdd) {
        HotelRoomOrder hotelRoomOrder = new HotelRoomOrder();

        Integer userId = renewRoomAdd.getUserId();
        // 根据roomId查询房间状态status 1.未预约 2.已预约
        Integer roomId = renewRoomAdd.getRoomId();
        Integer result = hotelRoomMapper.selectStatusById(roomId);
        if (result == 2) {
            hotelRoomOrder.setRoomId(roomId);
            hotelRoomOrder.setUserId(renewRoomAdd.getUserId());
            // 2表示续费房间
            hotelRoomOrder.setType(2);
            hotelRoomOrder.setInfo("续费房间");

            // 获取当前用户的vip信息,feign远程接口调用
            R my = vipFeign.my();
            // 判断vip中my方法返回的R中data的数据类型
            // 字符串说明有错,是map说明是正常流程
            if (!(my.getData() instanceof String)) {
                LinkedHashMap<String, Object> map = (LinkedHashMap<String, Object>) my.getData();
                Integer value = (Integer) map.get("type");
                if (value == 1 || value == 2 || value == 3) {
                    hotelRoomOrder.setVipType(value);
                } else {
                    hotelRoomOrder.setVipType(0);
                }
            } else {
                hotelRoomOrder.setVipType(0);
            }

            hotelRoomOrder.setPayType(renewRoomAdd.getPayType());

            // 创建订单号
            String no = "r" + System.currentTimeMillis() + RandomUtil.randomNumbers(6);
            hotelRoomOrder.setRoomNo(no);
            hotelRoomOrder.setStatus(1);

            // 根据user_id,room_id和type类型查询用户预约房间时的订单信息
            List<HotelRoomOrder> roomOrderList = hotelRoomOrderMapper.selectOrderById(userId, roomId);
            HotelRoomOrder reserveOrder = roomOrderList.get(0);
            if (reserveOrder == null) {
                return null;
            }

            // 获取原订单开始时间sdate和结束时间edate
            Date edate = reserveOrder.getEdate();
            Date sdate = reserveOrder.getSdate();
            // 判断edate与现在时间
            if (edate.getTime() >= System.currentTimeMillis()) {
                // 订单还未开始或正在执行中,
                // 原订单数据修改结束时间和天数
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(edate);
                calendar.add(Calendar.DAY_OF_MONTH, renewRoomAdd.getDays());
                edate = calendar.getTime();
                reserveOrder.setEdate(edate);
                reserveOrder.setDays(reserveOrder.getDays() + renewRoomAdd.getDays());
            } else {
                // 订单已过期,更新开始时间和结束时间以及天数
                reserveOrder.setSdate(new Date());
                Calendar calendar = Calendar.getInstance();
                calendar.add(Calendar.DAY_OF_MONTH, renewRoomAdd.getDays());
                edate = calendar.getTime();
                reserveOrder.setEdate(edate);
                reserveOrder.setDays(renewRoomAdd.getDays());
            }

            // 修改原订单表的数据
            int num = hotelRoomOrderMapper.updateById(reserveOrder);
            if (num <= 0) {
                return null;
            }

            // 初始化天数和日期
            hotelRoomOrder.setSdate(sdate);
            hotelRoomOrder.setEdate(edate);
            hotelRoomOrder.setDays(renewRoomAdd.getDays());
            // 订单创建时间
            hotelRoomOrder.setCtime(new Date());

            int i = hotelRoomOrderMapper.insert(hotelRoomOrder);
            if (i <= 0) {
                return null;
            }

            return hotelRoomOrder;
        }

        return null;
    }
}
